{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn import tree\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_table('lenses.txt',header=None)\n",
    "df.columns=['age','prescript','astigmatic','tearRate','type']\n",
    "\n",
    "clf = DecisionTreeClassifier(criterion='entropy')\n",
    "\n",
    "data = df.copy()\n",
    "cols = df.shape[1]\n",
    "for index in range(cols-1):\n",
    "    labels =df.iloc[:,index].unique()\n",
    "    for label_index in range(len(labels)):\n",
    "        mask = data.iloc[:,index]==labels[label_index]\n",
    "        data.iloc[mask,index]=label_index\n",
    "\n",
    "X = np.array(data.iloc[:,:-1])\n",
    "Y= np.array(data.iloc[:,-1])\n",
    "clf.fit(X,Y)\n",
    "plt.figure(figsize=(200,100))\n",
    "tree.plot_tree(clf,feature_names=df.columns,class_names=data.iloc[:,-1].unique())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(\"lenses\",clf)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d44d76ef8cbbc4331cecfe2e59228ac31ebb71026289858a116838be7168b60b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
